﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models.Tablets;
using AceMobile.Models;

namespace AceMobile.Controllers.Admin
{
    [Authorize(Roles = "Administrator,Tabletmanager")]
    public class TabletAdminController : Controller
    {
        private MobileWebContext db = new MobileWebContext();

        //
        // GET: /TabletAdmin/

        public ViewResult Index()
        {
            var tablets = db.Tablets.Include(t => t.TabletOS).Include(t => t.TabletProducer);
            return View(tablets.ToList());
        }

        //
        // GET: /TabletAdmin/Details/5

        public ViewResult Details(int id)
        {
            Tablet tablet = db.Tablets.Find(id);
            return View(tablet);
        }

        //
        // GET: /TabletAdmin/Create

        public ActionResult Create()
        {
            ViewBag.TabletOSID = new SelectList(db.TabletOSs, "ID", "Name");
            ViewBag.TabletProducerID = new SelectList(db.TabletProducers, "ID", "Name");
            return View();
        } 

        //
        // POST: /TabletAdmin/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Tablet tablet)
        {
            if (ModelState.IsValid)
            {
                db.Tablets.Add(tablet);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.TabletOSID = new SelectList(db.TabletOSs, "ID", "Name", tablet.TabletOSID);
            ViewBag.TabletProducerID = new SelectList(db.TabletProducers, "ID", "Name", tablet.TabletProducerID);
            return View(tablet);
        }
        
        //
        // GET: /TabletAdmin/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tablet tablet = db.Tablets.Find(id);
            ViewBag.TabletOSID = new SelectList(db.TabletOSs, "ID", "Name", tablet.TabletOSID);
            ViewBag.TabletProducerID = new SelectList(db.TabletProducers, "ID", "Name", tablet.TabletProducerID);
            return View(tablet);
        }

        //
        // POST: /TabletAdmin/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Tablet tablet)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tablet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TabletOSID = new SelectList(db.TabletOSs, "ID", "Name", tablet.TabletOSID);
            ViewBag.TabletProducerID = new SelectList(db.TabletProducers, "ID", "Name", tablet.TabletProducerID);
            return View(tablet);
        }

        //
        // GET: /TabletAdmin/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tablet tablet = db.Tablets.Find(id);
            return View(tablet);
        }

        //
        // POST: /TabletAdmin/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tablet tablet = db.Tablets.Find(id);
            db.Tablets.Remove(tablet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}